package com.orionlily.redis3t.tool.jacksonserializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

/**
 * LocalTime的json序列化器
 * <年月日> 为 <当天年月日>
 *
 * @author li.lc
 */
public class LocalTimeSerializer extends JsonSerializer<LocalTime> {
    @Override
    public void serialize(LocalTime localTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now(zoneId);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        jsonGenerator.writeNumber(localDateTime.atZone(zoneId).toInstant().toEpochMilli());
    }
}
